#include "stdafx.h"
#include "Demo.h"

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE prevInstance,
				   PSTR cmdLine, int showCmd)
{
 	#if defined(DEBUG) | defined(_DEBUG)
		_CrtSetDbgFlag( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );
	#endif

	cGameDemo app(hInstance, "Gunners Heart", D3DDEVTYPE_HAL, D3DCREATE_HARDWARE_VERTEXPROCESSING);
	gd3dApp = &app;

	DirectInput di(DISCL_NONEXCLUSIVE|DISCL_FOREGROUND, DISCL_NONEXCLUSIVE|DISCL_FOREGROUND);
	gDInput = &di;

	return gd3dApp->run();
}
//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
cGameDemo::cGameDemo(HINSTANCE hInstance, std::string winCaption, D3DDEVTYPE devType, DWORD requestedVP)
: cD3DApplication(hInstance, winCaption, devType, requestedVP)
{
	if(!checkDeviceCaps())
	{
		MessageBox(0, "checkDeviceCaps() Failed", 0, 0);
		PostQuitMessage(0);
 	}

	mGfxStats = new GfxStats;
	mFont = mGfxStats->mFont;
	
 	onResetDevice();
}
//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
cGameDemo::~cGameDemo()
{
 	SAFE_DELETE(mGfxStats);
}
//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
LRESULT cGameDemo::msgProc(UINT msg, WPARAM wParam, LPARAM lParam)
{
	switch (msg)
	{
		case WM_LBUTTONDOWN:
		case WM_RBUTTONDOWN:
		case WM_LBUTTONUP:
		case WM_LBUTTONDBLCLK:
		case WM_RBUTTONUP:
		case WM_RBUTTONDBLCLK:
		case WM_MOUSEMOVE:
 		{
			mMouse.x = LOWORD(lParam);
			mMouse.y = HIWORD(lParam);
		}
		break;
	}
	return cD3DApplication::msgProc(msg, wParam, lParam);
}
//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
bool cGameDemo::checkDeviceCaps()
{
	return true;
}
//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
void cGameDemo::onLostDevice()
{
	mPlayGame.SafeDelete();
}
//mGrid.set(1.f, 20.f);
void cGameDemo::onResetDevice() 
{
	mPlayGame.OnLoadModel();
	mPlayGame.OnLoadUI();

}
//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
void cGameDemo::updateScene(float dt)
{
	gDInput->poll();
	mGfxStats->update(dt);
	gGameTime.FPS = mGfxStats->getFPS();
	mPlayGame.OnUpdate( dt );
}
//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
void cGameDemo::drawScene(float dt)
{
	setupProjMatrix();
	HR(gd3dDevice->SetTransform(D3DTS_VIEW, &mCamera.getViewTM()));
	setupLight(dt);
	HR(gd3dDevice->Clear(0, 0, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB( 0, 0, 0 ), 1.0f, 0));
	HR(gd3dDevice->BeginScene());
	//---Light Set
	offLight();//Light Off
	onLight();//Light On
	mGfxStats->display();
	//3d_Object Draw
	mPlayGame.ModelRender( false );
	//--Interface Draw Start
	//-----Interface Draw End
	HR(gd3dDevice->EndScene());
	HR(gd3dDevice->Present(0, 0, 0, 0));
}
//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
void cGameDemo::setupProjMatrix()
{
	float w = (float)md3dPP.BackBufferWidth;
	float h = (float)md3dPP.BackBufferHeight;
	D3DXMatrixPerspectiveFovLH(&mProj, D3DX_PI * 0.35f, w/h, 1.0f, 1000.0f);
	HR(gd3dDevice->SetTransform(D3DTS_PROJECTION, &mProj));
}
//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
void cGameDemo::drawText( int x, int y, const char* txt, D3DCOLOR color )
{
	RECT R = {x, y, 0, 0};
	HR(mFont->DrawText(0, txt, -1, &R, DT_NOCLIP, color));
}
//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
void cGameDemo::setupLight(float dt)
{
	static float ftime = 0.f ;

	ftime += dt / 5.f ;
	if (ftime > 1000.0f) ftime = 0.f ;

	float fAngle = ftime * 2.0f * D3DX_PI ;
	D3DXCOLOR color(1.0f, 1.0f, 1.0f, 1.0f);
	D3DXVECTOR3 dir(cosf(fAngle), -1.0f, sinf(fAngle));

	D3DLIGHT9 light;
	ZeroMemory(&light, sizeof(light));

	light.Type      = D3DLIGHT_DIRECTIONAL;
	light.Ambient   = color * 0.2f;
	light.Diffuse   = color * 0.8f;
	light.Specular  = color * 0.1f;
	D3DXVec3Normalize((D3DXVECTOR3*)&light.Direction, &dir);	
 	gd3dDevice->SetLight(0, &light);
	gd3dDevice->LightEnable(0, true);
	//gd3dDevice->SetRenderState(D3DRS_SPECULARENABLE, TRUE);
	gd3dDevice->SetRenderState(D3DRS_LIGHTING, TRUE);
}
//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
void cGameDemo::offLight()
{
	gd3dDevice->SetRenderState(D3DRS_LIGHTING, FALSE);
}
//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
void cGameDemo::onLight()
{
	gd3dDevice->SetRenderState(D3DRS_LIGHTING, TRUE);
}
